package com.example.stlink.fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.stlink.R;
import com.example.stlink.utils.activityUtil.TeaClassListUtil;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class TeaCourseListFragment extends Fragment {

    private static TeaClassListUtil teaClassListUtil;

    private static FloatingActionButton ftBtRefresh;

    private static AppCompatActivity mAppCompatActivity;
    private static ViewPager2 viewPager2;

    private static ActivityResultLauncher<Intent> intentActivityResultLauncher;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tea_course_list, container, false);
        initView(view);
        initPage();
        return view;
    }

    public static TeaCourseListFragment newInstance(String str, AppCompatActivity appCompatActivity){
        TeaCourseListFragment frag = new TeaCourseListFragment();
        mAppCompatActivity = appCompatActivity;

        intentActivityResultLauncher = appCompatActivity.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            Intent data = result.getData();
            int resultCode = result.getResultCode();
            System.out.println("registerForActivityResult...........................");
            if (resultCode == Activity.RESULT_OK) {

            }
        });
        return frag;
    }

    /**
     * 初始化界面
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    private void initView(View view){

        teaClassListUtil = new TeaClassListUtil(view, mAppCompatActivity);
        ftBtRefresh = view.findViewById(R.id.ft_bt_refresh);
        viewPager2 = view.findViewById(R.id.nav_course_list_fragment);

        refreshData();

        setClickListener();
    }

    private static void initPage(){

        List<Fragment> list = new ArrayList<>();
        CrsUnfinishedListFragment crsUnfinishedListFragment = CrsUnfinishedListFragment.newInstance("未结课", mAppCompatActivity);
        CrsFinishedListFragment crsFinishedListFragment = CrsFinishedListFragment.newInstance("已结课", mAppCompatActivity);
        list.add(crsUnfinishedListFragment);
        list.add(crsFinishedListFragment);
        teaClassListUtil.initPage(list);
    }

    /**
     * 刷新数据
     */
    private void refreshData(){

    }

    /**
     * 为当前布局上的按钮添加点击监听事件
     */
    private void setClickListener(){
        //点击刷新页面
        ftBtRefresh.setOnClickListener(v -> {
            int position = viewPager2.getCurrentItem();
            if (position == 0){
                CrsUnfinishedListFragment.refreshData();
            }else{
                CrsFinishedListFragment.refreshData();
            }
        });
    }
}
